//
//  ViewController.m
//  SXH
//
//  Created by 房 国生 on 15/6/23.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "BaseViewController.h"
#import "CommonService.h"

@interface BaseViewController ()
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //选择自己喜欢的颜色
    if (is_IOS_7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.fd_prefersNavigationBarHidden = YES;
}

#pragma mark - 创建返回按钮
- (void)createLeftNavItem:(NSString *)imageName lightImage:(NSString *)lightName {
    //self.isLeft = YES;
//    UIImage *backImage=[UIImage imageNamed:imageName];
//    UIImage *lightImage=[UIImage imageNamed:lightName];
//    UIBarButtonItem *leftBarButtonItem = [self barButtonWithImage:backImage highlightImage:lightImage style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
//    
//    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
//    UIButton *backButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
//    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
//    [backButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//    backButton.titleLabel.font = [UIFont systemFontOfSize:14];
//    
//    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//    self.navigationItem.leftBarButtonItem = backButtonItem;
}

#pragma mark 返回按钮点击事件
- (void)goBack {
    if (self.isCancleButton) {
        [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    }else if(self.navigationController.viewControllers.count>1){
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - 根据图片创建UIBarButtonItem
- (id)barButtonWithImage:(UIImage *)image highlightImage:(UIImage *)lightImage style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, MAX(70, image.size.width/2), MAX(44, image.size.height/2));
    if (image) {
        [button setImage:image forState:UIControlStateNormal];
    }
    if (lightImage) {
        [button setImage:lightImage forState:UIControlStateHighlighted];
        [button setImage:lightImage forState:UIControlStateSelected];
    }
    
    if (is_IOS_7) {
        if (self.isLeft) {
            [button setImageEdgeInsets:UIEdgeInsetsMake(0,-37, 0, 0)];
        }else{
            [button setImageEdgeInsets:UIEdgeInsetsMake(0,0, 0, -28)];
        }
    }
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    return barButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showLoadingView:(BOOL)flag {
    
    if (flag == YES) {
        HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.delegate = self;
        HUD.labelText = Loading_Hard_Text;
    } else {
        [HUD hide:YES];
    }
}

- (void)showTips:(NSString*)message
{
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = message;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的位置，不指定则在屏幕中间显示
    //    HUD.yOffset = 100.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD show:YES];
    [HUD hide:YES afterDelay:1.0];
}

- (void) getAPPVersion:(BOOL)showMessage AppId:(NSString *)appId{
    
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           appId, @"id", nil];
    
    [service getNetWorkData:param
                       Path:APP_UPDATE
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      NSNumber *code = (NSNumber *)[dic objectForKey:@"code"];
                      if ([code intValue] == 0) {
                          NSDictionary *row = [dic objectForKey:@"row"];
                          NSString *lastVersion = [row objectForKey:@"version"];
                          _appURL = [row objectForKey:@"url"];
                          
                          NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
                          // app版本
                          NSString *currentVersion = [infoDictionary objectForKey:@"CFBundleVersion"];
                          if ([lastVersion isEqualToString:currentVersion] == NO) {
                              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新，是否前往更新？" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
                              alert.tag = 90000;
                              [alert show];
                          }
                          
                          if ([lastVersion isEqualToString:currentVersion] && showMessage) {
                              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                              alert.tag = 90001;
                              [alert show];
                          }
                          
                      } else {
                          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"未检查到版本相关信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                          alert.tag = 90001;
                          [alert show];
                      }
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      NSLog(@"1234====");
                  }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 90000) {
        if (buttonIndex == 1) {
            NSURL *url = [NSURL URLWithString:_appURL];
            [[UIApplication sharedApplication]openURL:url];
        }
    }
}

-(void)handlePayResult:(NSString *)code {
    
}

-(void)hideStatusBar
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
}
-(void)showStausBar
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
}
@end
